package com.gitee.huxing.course;

import cn.hutool.core.date.DateUtil;
import com.gitee.huxing.course.controller.ProducerController;
import com.gitee.huxing.course.listener.QueueConsumerListener;
import com.gitee.huxing.course.listener.TopicConsumerListener;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.Bean;

import javax.annotation.Resource;


@Slf4j
@SpringBootApplication
public class SpringBootMqApplication implements CommandLineRunner {

    @Resource
    ProducerController producerController;

    public static void main(String[] args) {
        SpringApplication.run(SpringBootMqApplication.class, args);
    }

    @Override
    public void run(String... args) throws Exception {
        log.info("系统服务启动: {}", DateUtil.now());
        int index = 0;
        while (index++ < 10){
            // 发送queue消息
            producerController.sendQueue(index + " message.Queue");
            // 发送topic消息
            producerController.sendTopic(index + "message.Topic");
        }
    }

    @Bean(value = "queueConsumerListener")
    public QueueConsumerListener queueConsumerListener(){
        return new QueueConsumerListener();
    }

    @Bean(value = "topicConsumerListener")
    public TopicConsumerListener topicConsumerListener(){
        return new TopicConsumerListener();
    }

    @Bean
    public ProducerController getProducerController(){
        return new ProducerController();
    }
}
